

----------------8/25增加流量 增加每个设备最后一次过车信息存入redis key中

------gc_num 累计总数                   取的时候记得要删除
------gc_numbysec_00-59 按秒累计
------gc_numbyhour_00-23
------gc_wznumbysec_00-59
------gc_wznumbyhour_00-59
------gc_numbyday_00-31
------gc_lkbh_numbyday_00-31
------gc_lkbh_wznumbyday_00-31
------ip_enddata            每个设备最后一条上传记录


local tb_csys = {}
tb_csys[0]= "其它色"
tb_csys[1]= "白色"
tb_csys[2]= "银色"
tb_csys[3]= "灰色"
tb_csys[4]= "黑色"
tb_csys[5]= "红色"
tb_csys[6]= "深蓝"
tb_csys[7]= "蓝色"
tb_csys[8]= "黄色"
tb_csys[9]= "绿色"
tb_csys[10]= "棕色"
tb_csys[11]= "粉色"
tb_csys[12]= "紫色"
local tb_clpp = {}
tb_clpp[0]= "其它"
tb_clpp[1]= "大众"
tb_clpp[2]= "别克"
tb_clpp[3]= "宝马"
tb_clpp[4]= "本田"
tb_clpp[5]= "标致"
tb_clpp[6]= "丰田"
tb_clpp[7]= "福特"
tb_clpp[8]= "日产"
tb_clpp[9]= "奥迪"
tb_clpp[10]= "马自达"
tb_clpp[11]= "雪弗兰"
tb_clpp[12]= "雪铁龙"
tb_clpp[13]= "现代"
tb_clpp[14]= "奇瑞"
local tb_wfxwdm = {}
tb_wfxwdm[0]= "00000" --"正常"
tb_wfxwdm[1]= "13030"--"低速"
--tb_wfxwdm[2]= "超速"
tb_wfxwdm[3]= "13010" --"逆行"
tb_wfxwdm[4]= "16251" --"闯红灯"
--tb_wfxwdm[5]= "压车道线"
--tb_wfxwdm[6]= "不按导向"
--tb_wfxwdm[7]= "路口滞留"
tb_wfxwdm[8]= "10180" --"机占非"
tb_wfxwdm[9]= "12090" --"违法变道"
tb_wfxwdm[10]= "12080" --"不按车道"
tb_wfxwdm[11]= "13440" --"违法禁令"
--tb_wfxwdm[12]= "路口停车"
--tb_wfxwdm[13]= "绿灯停车"

local tb_hpys = {}
tb_hpys[0] = 2 --"蓝色"
tb_hpys[1] = 1 --"黄色"
tb_hpys[2] = 0 --"白色"
tb_hpys[3] = 3 --"黑色"
tb_hpys[4] = 4 --"其它"


local cjson                    = require "cjson"
local utils                    = require "gcs.utils"

------------初始化
local config_table = ngx.shared.gcs_config   --声明ngx.shared.gcs_config
local ipshine_table = ngx.shared.gcs_ipshine   --声明ngx.shared.gcs_ipshine
local form_timeout = config_table:get("p_form_timeout")
local form_chunk_size = config_table:get("p_form_chunk_size")
local mongo_table_name = "gcxx"
local format = "jpg"
local cjson_group = config_table:get("p_group_table")
local store_connect_timeout = config_table:get("p_store_connect_timeout")
local store_keep_timeout = config_table:get("p_store_keep_timeout")
local store_keep_count = config_table:get("p_store_keep_count")
local remote_ip                = ngx.var.remote_addr              --获得客户端ip
--utils.gcs_log("error","remote_ip:" ..remote_ip)
-----------校验参数
local tb_parameters = {}
tb_parameters.form_timeout = form_timeout
tb_parameters.form_chunk_size = form_chunk_size
tb_parameters.store_connect_timeout = store_connect_timeout
tb_parameters.store_keep_timeout = store_keep_timeout
tb_parameters.store_keep_count = store_keep_count
tb_parameters.cjson_group = cjson_group

local check_parameters = {
  "store_keep_timeout",                 "store_keep_count",                "cjson_group"
}

local res,err,error_no = utils.check_parameter(tb_parameters,check_parameters)
if not res then
  utils.gcs_log("error","second_line upload,check_parameter is err,err:" .. cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1001
  local message = err
  return ngx.say(cjson.encode(tb_return))
end

utils.gcs_log("debug","second_line upload,check_parameter is ok")


--------------------------加载函数---------------------

local red_method               = require "gcs.list.method"
local mongo_method             = require "gcs.mongodb.method"
local storage                  = require "resty.gcsdfs.storage"              --加载storage �?
local multipart                = require "resty.gcsdfs.multipart"
local up_utils                 = require "gcs.upload.upload_utils"
local up_hex                   = require "gcs.upload.hex"
local db_method                = require "gcs.db.method"
utils.gcs_log("debug","second_line upload,require complete")


---------------------------拼装信息----------------------------
---定义需要插入的字段放入table�?
--local lkbh,time,Hphm,hpys,xssd,xsfx,cd,wfxwdm,hmsztpzb,csys,clpp,sbbh,tpmc
--local gfile,filename,vssid
local db_data = {}
---获取�?
local form = multipart:new(form_timeout,form_chunk_size)
local res,err = form:parse()
if not res then
  ngx.req.discard_body()
  utils.gcs_log("error","second_line upload,form parse is nil,err:"..cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1002
  local message = err
  return ngx.say(cjson.encode(tb_return))
end
local form_res,err = form.fields
if not form_res then
  ngx.req.discard_body()
  utils.gcs_log("error","second_line upload,form_res is nil,err:"..cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1003
  local message = err
  return ngx.say(cjson.encode(tb_return))
end
---校验form_res中的??�段是否存�?
local res,err = up_utils.check_form_res(form_res)
if not res then
  ngx.req.discard_body()
  utils.gcs_log("error","second_line upload,check_form_res is err,err:"..cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1004
  local message = err
  return ngx.say(cjson.encode(tb_return))
end
utils.gcs_log("debug","second_line upload,check form_res is ok")

utils.gcs_log("debug","type:" .. cjson.encode(form_res.type))
--utils.gcs_log("debug","data:" .. cjson.encode(form_res.data))
---判断form_res.type.value
if form_res.type.value == "sdk" then      --卡口、电警 sdk方式传递
  --解析data流
  local str_data = form_res.data.value
  local ki,kj = string.find(str_data,"Plate")         --带plate的图片直接去掉
  if ki then
    ngx.req.discard_body()
    return
  end
  local fpath,fname = up_utils.get_filename(str_data)
  --解析fname
  local rt,err = up_utils.StringSplit(fname,"-")
  if not rt then
    local tb_return = {}
    local vssid = 0
    local code = 1005
    local message = err
    return ngx.say(cjson.encode(tb_return))
  end
  db_data.cd = rt[1]                              --车道
  db_data.time = rt[2]                            --时间
  db_data.zpxh = rt[3]                            --抓拍序号
  db_data.txxh = string.sub(rt[13],-3,-1)         --图像序号取最后3个
  if rt[4] == "" then
    db_data.hphm = "无车牌"                         --号牌号码
  else
    db_data.hphm = up_utils.createIconv("gbk","utf-8",rt[4]) or ""   --将gbk转成utf-8
    local tb_reys = {"蓝","黄","白","黑"}
    for _,v in pairs(tb_reys) do
      local i = string.find(db_data.hphm,v)
      if i then
        db_data.hphm = string.gsub(db_data.hphm,v,"",1)
      end
    end
  end
  db_data.hpys = tb_hpys[tonumber(rt[5])] or 4    --号牌颜色
  db_data.xssd = rt[6]                            --行驶速度

  local i,j = string.find(rt[14],"_")           --获取ip
  db_data.ip = string.sub(rt[14],1,i-1)


  ----------------根据客户端的Ip来判断是电警还是卡口
  if remote_ip == "128.127.110.40" or "128.127.110.98" then             --卡口
    local xs = ipshine_table:get("xs_"..db_data.ip) or "99999999"  --获取限速
    db_data.zpzs = ipshine_table:get("zpzs_"..db_data.ip) or "2"   --卡口抓拍张数为2
    local per_xs = tonumber(db_data.xssd)/tonumber(xs)             --现在均为非高速公路
    if tonumber(per_xs) > 1 then                                    --判断车速是否违法
      if tonumber(per_xs) < 1.2 then
        db_data.wfxwdm = "13030"
    else
      if tonumber(per_xs) < 1.5 then
        db_data.wfxwdm = "13031"
      else
        if tonumber(per_xs) < 1.7 then
          db_data.wfxwdm = "16030"
        else
          if tonumber(per_xs) < 2 then
            db_data.wfxwdm = "16031"
          else
            db_data.wfxwdm = "16032"
          end
        end
      end
    end
    else
      db_data.wfxwdm = "00000"
    end

  else                                             --电警
    db_data.wfxwdm = tb_wfxwdm[tonumber(rt[8])] or "00000"    --违法行为代码
    if db_data.wfxwdm == "00000" then
      db_data.zpzs = 1                                       --电警不违法，抓拍张数为一张
    else
      db_data.zpzs = 3                                       --电警违法，抓拍张数为三张
    end

  end

  ------------------------------------------------------------
  db_data.hpsztpzb = rt[9]                        --坐标
  db_data.csys = tb_csys[tonumber(rt[10])] or "其它色"   --车身颜色
  db_data.clpp = tb_clpp[tonumber(rt[11])] or "其它"    --车辆品牌
  db_data.lkbh = ipshine_table:get("lkbh_"..db_data.ip) or "lkbh"   --路口编号
  db_data.xsfx = ipshine_table:get("xsfx_"..db_data.ip) or "xsfx"   --行驶方向
  db_data.sbbh = ipshine_table:get("sbbh_"..db_data.ip) or "sbbh"   --设备编号
  db_data.tpmc = db_data.lkbh.."-"..db_data.time.."-"..db_data.txxh.."-"..db_data.zpzs.."-"..
    db_data.zpxh.."-"..db_data.hphm.."-"..db_data.hpys.."-"..
    db_data.xssd.."-"..db_data.xsfx.."-"..db_data.cd.."-"..db_data.wfxwdm.."-"..
    db_data.hpsztpzb.."-"..db_data.csys.."-"..db_data.clpp.."-"..db_data.sbbh
  db_data.filename = str_data
  db_data.id = db_data.lkbh .."_".. db_data.time.."_" .. db_data.sbbh
  db_data.size = form_res.gfile1.file_size
  db_data.md5 = form_res.gfile1.md5
  --存储storage
  local st_res = up_utils.get_storage(cjson_group)
  local tb_value = {}
  local tb_value = {
    timeout = store_connect_timeout,
    keep_timeout = store_keep_timeout,
    keep_count = store_keep_count
  }
  local gfile,err = up_utils.save_storage(form_res,st_res,tb_value,format)
  if not gfile then
    utils.gcs_log("error","second_line upload,save_storage is err,err:"..cjson.encode(err))
    local tb_return = {}
    local vssid = 0
    local code = 1006
    local message = err
    return ngx.say(cjson.encode(tb_return))
  end
  db_data.gfile = gfile
  db_data.str_res = cjson.encode(st_res)
  utils.gcs_log("debug","second_line upload,get param is ok")


elseif form_res.type.value == "ftp" then        ---部分电警设备ftp上传
  --获取文件�?文件名中包含需要的信息)
  utils.gcs_log("debug","time1")
  ------取出图片名,解析图片名
  local filename = form_res.gfile1.filename
  --解析fname
  local rt,err = up_utils.StringSplit(filename,"-")
  if not rt then
    local tb_return = {}
    local vssid = 0
    local code = 1005
    local message = err
    return ngx.say(cjson.encode(tb_return))
  end
  db_data.time = rt[1]
  db_data.txxh = rt[2]
  if rt[4] then
    db_data.wfxwdm = "16251"  --闯红灯
    db_data.zpzs = rt[3]      --抓拍张数
    db_data.zpxh = rt[4]      --抓拍序号
  else
    db_data.wfxwdm = "00000"  --正常
    db_data.zpzs = 1        --抓拍张数
    db_data.zpxh = 1        --抓拍序号
  end
  local _16hex = up_hex._to16hex(table.concat(form_res.gfile1.value)) --获取16进制二进制流
  utils.gcs_log("debug","time2")
  --取字符串中包含的最后一个"ffd9"字断以后的16进制字符串
  local re_str = _16hex
  while 1 do
    local i,j = string.find(re_str,"FFD9")
    if i then
      re_str = string.sub(re_str,j+1,-1)
    else
      if re_str == _16hex then
        re_str = nil
      end
      break
    end
  end
  utils.gcs_log("debug","time3")
  local _16hex_kkmc = string.sub(re_str,1,64) --取出16进制字符串中包含的车道信息(16进制)
  --local _16hex_hdsj = string.sub(re_str,65,88)   --红灯时间
  --local _16hex_pssj = string.sub(re_str,89,112) --拍摄时间
  local _16hex_cd   = string.sub(re_str,121,124)  --车道
  --------转16进制table
  local tb_16hex_kkmc = up_hex._to16tb_hex(_16hex_kkmc)
  --local tb_16hex_pssj = up_hex._to16tb_hex(_16hex_pssj)
  local tb_16hex_cd = up_hex._to16tb_hex(_16hex_cd)
  -------转十进制table
  local tb_10hex_kkmc = up_hex._to10tb_hex(tb_16hex_kkmc)
  --local tb_10hex_pssj = up_hex._to10tb_hex1(tb_16hex_pssj)
  local tb_10hex_cd = up_hex._to10tb_hex(tb_16hex_cd)
  local gbk_kkmc = string.char(unpack(tb_10hex_kkmc))
  utils.gcs_log("debug","time4")
  local utf8_kkmc = up_utils.createIconv("gbk","utf-8",gbk_kkmc) or ""
  utils.gcs_log("debug","time5")
  --------------------------------测试输出
  db_data.cd = table.concat(tb_10hex_cd)
  ---------通过kkmc以及cd向mysql中查到lkbh,sbbh,xsfx
  local rt1,err = up_utils.StringSplit(utf8_kkmc,"-")                                                   ------取出kkmc
  local query = "select * from gcs_ipshine where C_KKMC = "..ngx.quote_sql_str(rt[1])
  query = query .. " and C_CD like '%" .. db_data.cd ..",%;"
  local tb_query = db_method.query(query)
  if not next(tb_query) then
    utils.gcs_log("error","query: "..query)
    local tb_return = {}
    local vssid = 0
    local code = 1111
    local message = "mysql select is err"
    return ngx.say(cjson.encode(tb_return))
  end
  db_data.lkbh = tb_query[1].C_LKBH
  db_data.sbbh = tb_query[1].C_SBBH
  db_data.xsfx = tb_query[1].C_XSFS
  db_data.ip = tb_query[1].C_KEY
  db_data.hphm = "无"
  db_data.hpys = 4
  db_data.xssd = 0
  db_data.hpsztpzb = ""
  db_data.csys = "其它色"
  db_data.clpp = "其它"
  db_data.tpmc = db_data.lkbh.."-"..db_data.time.."-"..db_data.txxh.."-"..db_data.zpzs.."-"..
    db_data.zpxh.."-"..db_data.hphm.."-"..db_data.hpys.."-"..
    db_data.xssd.."-"..db_data.xsfx.."-"..db_data.cd.."-"..db_data.wfxwdm.."-"..
    db_data.hpsztpzb.."-"..db_data.csys.."-"..db_data.clpp.."-"..db_data.sbbh
  db_data.id = db_data.lkbh .."_".. db_data.time.."_" .. db_data.sbbh
  db_data.size = form_res.gfile1.file_size
  db_data.md5 = form_res.gfile1.md5
elseif form_res.type.value == "api" then
  return
else
  return
end

utils.gcs_log("debug","db_data is ok:"..cjson.encode(db_data))


--------------------------插入数据�?--------------------------------
---插入mongodb
local n,err = mongo_method.save_data(mongo_table_name,db_data)
if not n then
  utils.gcs_log("error","second_line upload,save mongodb is err,err:"..cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1007
  local message = err
  return ngx.say(cjson.encode(tb_return))
end

---插入redis,key,value
local insert_data = {}
insert_data[db_data.id] = cjson.encode(db_data)
local res,err = red_method.set_data(insert_data)
if not res then
  utils.gcs_log("error","second_line upload,save redis is err,err:"..cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1015
  local message = err
  return ngx.say(cjson.encode(tb_return))
end

---插入list
if form_res.type.value == "sdk" then
  if remote_ip == "128.127.110.42" or "128.127.110.98" then          -----卡口
    if db_data.wfxwdm == "00000" then
      local res,err = red_method.push("l","kklist",db_data.id) --卡口sdk list
      if not res then
        utils.gcs_log("error","second_line upload,push kklist is err,err:"..cjson.encode(err))
        local tb_return = {}
        local vssid = 0
        local code = 1008
        local msg = err
        return ngx.say(cjson.encode(tb_return))
      end
  else
    local res,err = red_method.push("l","cslist",db_data.id)
    if not res then
      utils.gcs_log("error","second_line upload,push cslist is err,err:"..cjson.encode(err))
      local tb_return = {}
      local vssid = 0
      local code = 1009
      local msg = err
      return ngx.say(cjson.encode(tb_return))
    end
    up_utils.ins_numbysec("gc_wznumbysec_")       --按秒累计违章过车
    up_utils.ins_numbyhour("gc_wznumbyhour_")     --按小时累计违章过车
    up_utils.ins_numbyday("gc_"..db_data.lkbh.."_wznumbyday_") --按天,路口编号累计违章
  end
  else                             --------电警
    if db_data.wfxwdm == "00000" then
      local res,err = red_method.push("l","hkdjlist",db_data.id) --卡口sdk list
      if not res then
        utils.gcs_log("error","second_line upload,push hkdjlist is err,err:"..cjson.encode(err))
        local tb_return = {}
        local vssid = 0
        local code = 1008
        local msg = err
        return ngx.say(cjson.encode(tb_return))
      end
  else
    local res,err = red_method.push("l","hkwzlist",db_data.id)
    if not res then
      utils.gcs_log("error","second_line upload,push hkwzlist is err,err:"..cjson.encode(err))
      local tb_return = {}
      local vssid = 0
      local code = 1009
      local msg = err
      return ngx.say(cjson.encode(tb_return))
    end
    up_utils.ins_numbysec("gc_wznumbysec_")       --按秒累计违章过车
    up_utils.ins_numbyhour("gc_wznumbyhour_")     --按小时累计违章过车
    up_utils.ins_numbyday("gc_"..db_data.lkbh.."_wznumbyday_") --按天,路口编号累计违章
  end                                                      --电警
  end
elseif form_res.type.value == "ftp" then    --插入文通电警 list
  if db_data.wfxwdm == "00000" then
    local res,err = red_method.push("l","wtdjlist",db_data.id)
    if not res then
      utils.gcs_log("error","second_line upload,push kklist is err,err:"..cjson.encode(err))
      local tb_return = {}
      local vssid = 0
      local code = 1011
      local msg = err
      return ngx.say(cjson.encode(tb_return))
    end
else
  local res,err = red_method.push("l","wtwzlist",db_data.id)
  if not res then
    utils.gcs_log("error","second_line upload,push wzlist is err,err:"..cjson.encode(err))
    local tb_return = {}
    local vssid = 0
    local code = 1012
    local msg = err
    return ngx.say(cjson.encode(tb_return))
  end
end
end
utils.gcs_log("info","second_line upload,upload is ok")




----------
up_utils.ins_numbysec("gc_numbysec_")     --按秒累计过车
up_utils.ins_numbyhour("gc_numbyhour_")     --按小时累计过车
up_utils.ins_numbyday("gc_numbyday_")      --按天累计过车
up_utils.ins_numbyday("gc_"..db_data.lkbh.."_numbyday_")      --按路口名称累计过车
---------------累计过车总数
local tb_gc_num = {gc_num=1}
local res,err = red_method.incr_data(tb_gc_num)


--------------插入设备最后一条上传数据
local key = db_data.ip.."_enddata"
local ins_data = {}
ins_data[key] = cjson.encode(db_data)
local res,err = red_method.set_data(ins_data)



